package com.java.wmovie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.wmovie.entity.Movie;
import com.java.wmovie.entity.Orders;
import com.java.wmovie.entity.PageUtils;
import com.java.wmovie.entity.User;
import com.java.wmovie.mapper.MovieMapper;
import com.java.wmovie.mapper.OrdersMapper;
import com.java.wmovie.mapper.UserMapper;
import com.java.wmovie.service.MovieService;
import com.java.wmovie.service.OrdersService;
import com.java.wmovie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import javax.swing.text.Style;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhw
 * @since 2022-01-27
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrdersService ordersService;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    MovieMapper movieMapper;


    @RequestMapping("/control")
    public String control(HttpSession session, Model model){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        PageUtils pageUtils=new PageUtils(new Page(1,1),"/control");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("u_name", name);
        Page<User> page=userMapper.selectPage(pageUtils.getPage(),wrapper);
        model.addAttribute("page",page);
        return "/control";
    }

    @RequestMapping("/control1")
    public String control1(HttpSession session, Model model, @RequestParam(value = "i",defaultValue = "1")Integer i){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        String name1=userMapper.selectOne(new QueryWrapper<User>().eq("u_name",name)).getUName();
        PageUtils pageUtils=new PageUtils(new Page(i,3),"control1");
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.like("u_name", name1);
        Page<Orders> page=ordersMapper.selectPage(pageUtils.getPage(),wrapper);
        System.out.println(page.getRecords());
        model.addAttribute("page",page);
        return "/control1";
    }

    @RequestMapping("/control1/delete")
    public String Control1Delete(@RequestParam("id")Integer id){
        boolean b=ordersService.removeById(id);
        System.out.println("删除---->"+b);
        return "redirect:/user/control1";
    }

    @RequestMapping("/control1/see")
    public String Control1See(Model model,HttpSession session,@RequestParam("id")Integer id){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        Orders orders=ordersService.getById(id);
        String MName=orders.getMName();
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.like("name",MName);
        List<Movie> movies=movieMapper.selectList(wrapper);
        model.addAttribute("movie",movies);
        model.addAttribute("orders",orders);
        return "/buy4";
    }

    @RequestMapping("/control2")
    public String control2(HttpSession session, Model model){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("u_name",name));
        model.addAttribute("user",user);
        session.setAttribute("userid",user.getUId());
        return "/control2";
    }

    @RequestMapping("/control2s")
    public String control2s(HttpSession session, Model model,User user) {
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        Integer id1=(Integer) session.getAttribute("userid");
        user.setUId(id1);
        userService.updateById(user);
        System.out.println("修改完成");
        return "redirect:control";
    }


}

